import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import dts from 'vite-plugin-dts'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    dts({
      copyDtsFiles: true,
      beforeWriteFile(filePath, content) {
        return {
          filePath: filePath.replace(
            `lemon-ui/packages/`,
            `lemon-ui/`
          ),
          content,
        }
      },
      include: './packages/'
    }),
    VueSetupExtend(),
  ],
  build: {
    target: 'modules',
    //打包文件目录
    outDir: 'lemon-ui',
    //压缩
    minify: true,
    //css分离
    // cssCodeSplit: true,
    rollupOptions: {
      //忽略打包vue、element-plus
      external: [
        'vue',
        'element-plus',
        '@element-plus/icons-vue',
        'lodash-es',
        '@vue/shared',
      ],
      // input: ['index.ts'],
      output: [
        {
          //打包格式
          format: 'es',
          //打包后文件名
          entryFileNames: '[name].mjs',
          //让打包目录和我们目录对应
          preserveModules: true,
          exports: 'named',
          // //配置打包根目录
          // dir: '../../lemon-ui/es',
        },
        {
          //打包格式
          format: 'cjs',
          //打包后文件名
          entryFileNames: '[name].js',
          //让打包目录和我们目录对应
          preserveModules: true,
          exports: 'named',
          // //配置打包根目录
          // dir: '../../lemon-ui/lib',
        },
      ],
    },
    lib: {
      entry: resolve(__dirname, './packages/index.ts'),
      name: 'lemon-ui',
      fileName: 'lemon-ui',
    },
  },
})
